//
//  WCG_O_SectionHeaderViewController.m
//  Project_B
//
//  Created by 黑黑 on 15/11/25.
//  Copyright © 2015年 Atom_Wang. All rights reserved.
//

#import "WCG_O_SectionHeaderViewController.h"
#import "WCG_O_SectonHeaderTableViewCell.h"
#import "WCG_O_DetailViewController.h"
#import "GXiaNetHandle.h"
#import "Masonry.h"
#import "MBProgressHUD.h"
@interface WCG_O_SectionHeaderViewController () <UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property (nonatomic, strong) UIView *navigationView;
@property (nonatomic, strong) UIImageView *yellowImage;
@property (nonatomic, strong) UILabel *charLable;
@property (nonatomic, strong) UILabel *engLable;
@property (nonatomic, strong) UILabel *TitleLable;
@property (nonatomic, strong) NSMutableArray *specialListArr;

@end

@implementation WCG_O_SectionHeaderViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    [self createTableView];
    [self getSpecialHeaderDetailData];
    [self addToTitleView];
    [self loadNightMBProgress];
    // 创建底部按钮
    [UIView animateWithDuration:1.5 animations:^{
        [self createButtomBackButtonIsHiddenShareBtn:YES];
        [self.backBtn addTarget:self action:@selector(backTofront) forControlEvents:UIControlEventTouchUpInside];
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSArray *temp = self.tabBarController.view.subviews;
    UIView *tabbarView = temp[2];
    [UIView animateWithDuration:1 animations:^{
        tabbarView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 49);
    }];
    self.navigationController.navigationBarHidden = YES;
}

#pragma mark - 获取数据
- (void)getSpecialHeaderDetailData {
    NSString *url = [NSString stringWithFormat:kSpecialHeaderWeb, self.categoryModel.category_id];
    [GXiaNetHandle getUrl:url cookie:kSpecialHeaderCookie success:^(id result) {
        self.specialListArr = [GXSpecialModel transformWithArray:result[@"special_list"]];
        [self.tableView reloadData];
        [self.hud removeFromSuperview];
    } failure:^(NSError *error) {
        
    }];
}

- (void)createTableView
{
    [self createTableViewWithFrame:CGRectMake(0, 84, kScreenWidth, kScreenHeight - 84)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerClass:[WCG_O_SectonHeaderTableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (void)addToTitleView
{
//    self.navigationController.navigationItem.titleView
    self.navigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, kScreenWidth, 64)];
    self.navigationView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.navigationView];
    
    self.yellowImage = [[UIImageView alloc] initWithFrame:CGRectZero];
    [self.navigationItem.titleView addSubview:self.yellowImage];
    self.yellowImage.image = [UIImage imageNamed:@"icon-special-number"];
    [self.navigationView addSubview:self.yellowImage];
    [self.yellowImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.navigationView.mas_left).with.offset(15);
        make.centerY.mas_equalTo(self.navigationView.mas_centerY);
        make.height.mas_equalTo(42);
        make.width.mas_equalTo(42);
    }];
    
    self.charLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    self.charLable.text = [self.categoryModel.category_ename substringToIndex:1];
    [self.yellowImage addSubview:self.charLable];
    [self.charLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.yellowImage.mas_centerX);
        make.centerY.mas_equalTo(self.yellowImage.mas_centerY);
        
    }];
    
    self.engLable = [[UILabel alloc] initWithFrame:CGRectZero];
    self.engLable.text = self.categoryModel.category_ename;
    self.engLable.font = [UIFont systemFontOfSize:12];
    [self.navigationView addSubview:self.engLable];
    [self.engLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.yellowImage.mas_top).with.offset(0);
        make.left.mas_equalTo(self.yellowImage.mas_right).with.offset(10);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(18);
    }];
    
    self.TitleLable = [[UILabel alloc] init];
    self.TitleLable.font = [UIFont systemFontOfSize:14];
    self.TitleLable.text = self.categoryModel.category_name;
    [self.navigationView addSubview:self.TitleLable];
    [self.TitleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.yellowImage.mas_right).with.offset(10);
        make.top.mas_equalTo(self.engLable.mas_bottom).with.offset(0);
        make.width.mas_equalTo(150);
        make.bottom.mas_equalTo(self.yellowImage.mas_bottom).with.offset(0);
    }];
}
#pragma mark - 返回按钮点击事件
- (void)backTofront
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - tableView协议事件
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.specialListArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WCG_O_SectonHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.specialModel = self.specialListArr[indexPath.row];
    return cell;
}
#pragma mark - UITableView点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WCG_O_DetailViewController *detailVC = [[WCG_O_DetailViewController alloc] init];
    
    detailVC.specialID = [self.specialListArr[indexPath.row] special_id];
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - scrollView代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
        [UIView animateWithDuration:0.3 animations:^{
            self.backBtn.alpha = 0;
            
        }];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [UIView animateWithDuration:0.3 animations:^{
        self.backBtn.alpha = 0.8;
    }];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [UIView animateWithDuration:0.3 animations:^{
        self.backBtn.alpha = 0.8;
    }];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
